package org.kjk.mail;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.kjk.mail.gmail.GMail;
import org.kjk.mail.hanmail.HanMail;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MailController {

	@Resource(name = "gmail")
	public GMail gmail;
	
	@Resource(name = "hanmail")
	public HanMail hmail;
	
	@RequestMapping(value = "/readMail.do", method = RequestMethod.GET)
	public String readMail(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return redirect(request, "/readGMail.do", "/readHMail.do");
	}

	@RequestMapping(value = "/readJsonMail.do", method = RequestMethod.GET)
	public void readJsonContent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String pass = request.getParameter("pass");
		System.out.println("id : " + id + " : " + pass);
		JSONObject subject = null;
		if (id == null) {
			id = (String) request.getSession().getAttribute("id");
			pass = (String) request.getSession().getAttribute("pass");
			if (id != null) {
				if (id.contains("@gmail.com")) {
					subject = gmail.getJsonSubject();
				} else if (id.contains("@hanmail.net")) {
					subject = hmail.getJsonSubject();
				}
				
			} else {
				return;
			}
		} else {
			request.getSession().setAttribute("id", id);
			request.getSession().setAttribute("pass", pass);
			System.out.println("Page Loading");
			if (id.contains("@gmail.com")) {
				if (id != null && pass != null) {
					gmail.setAccountDetails(id, pass);
				}
				subject = gmail.getJsonSubject();
			} else if (id.contains("@hanmail.net")) {
				if (id != null && pass != null) {
					hmail.setAccountDetails(id, pass);
				}
				subject = hmail.getJsonSubject();
			}
			System.out.println("page load success");
		}
		response.getWriter().println(subject);
	}
	
	@RequestMapping(value = "/readMailContent.do", method = RequestMethod.GET)
	public String readMailContent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return redirect(request, "/readGMailContent.do", "/readHMailContent.do");
	}

	@RequestMapping(value = "/readJsonMailContent.do", method = RequestMethod.GET)
	public String readJsonMailContent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return redirect(request, "/readJsonGMailContent.do", "/readJsonHMailContent.do");
	}
	
	@RequestMapping(value = "/mailLogin.do", method = RequestMethod.POST)
	public String gmailLogin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return redirect(request, "/gmailLogin.do", "/hmailLogin.do");
	}

	@RequestMapping(value = "/sendMail.do", method = RequestMethod.POST)
	public String sendMail(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return redirect(request, "/sendGMail.do", "/sendHMail.do");
	}

	@RequestMapping(value = "/writeMailRedirect.do", method = RequestMethod.GET)
	public String writeMailRedirect(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return "/WEB-INF/mail/receivemail.jsp";
	}
	
	public String redirect(HttpServletRequest request, String url1, String url2) {
		String id = request.getParameter("id");
		if (id == null) {
			id = (String) request.getSession().getAttribute("id");
			if (id != null) {
				if (id.contains("@gmail.com")) {
					return url1;
				} else if (id.contains("@hanmail.net")) {
					return url2;
				} else {
					return null;
				}
			} else {
				return null;
			}
		}
		if (id.contains("@gmail.com")) {
			return url1;
		} else if (id.contains("@hanmail.net")) {
			return url2;
		} else {
			return null;
		}
	}

}
